package pailiewu.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Count3 {


	private int[] count3List;

	private int[] count3LastList;

	private int[] count3AverageList;
	
	public int[] getCount3AverageList() {
		return count3AverageList;
	}

	private int[] count3ModList;
	
	private int maxMod;

	public int getMaxMod() {
		return maxMod;
	}

	public int[] getCount3LastList() {
		return count3LastList;
	}

	public void setCount3LastList(int[] count3LastList) {
		this.count3LastList = count3LastList;
	}

	private int num;

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

//	public static void main(String[] args) throws Exception {
//		// TODO Auto-generated method stub
//		HttpUtil.download();
//		Count3 count3 = new Count3();
//		count3.count();
//	}

	public int[] getCount3List() {
		return count3List;
	}

	public void setCount3List(int[] count3List) {
		this.count3List = count3List;
	}

	public void initCount3List() {
		count3List = new int[1000];
		count3LastList = new int[1000];
		count3AverageList = new int[1000];
		count3ModList = new int[1000];
		for (int i = 0; i < 1000; i++) {
			count3List[i] = 0;
			count3LastList[i] = 0;
			count3AverageList[i] = 0;
			count3ModList[i] = 0;
		}
	}

	public void count(String fileStr,String count3Str) throws Exception {

		FileReader fr = new FileReader(fileStr);
		BufferedReader br = new BufferedReader(fr);

		initCount3List();
		num = 0;
		maxMod=0;
		String line = "";
		while ((line = br.readLine()) != null) {
			num++;
			add(line);
			addLast(line, num);

		}

		for (int i = 0; i < 1000; i++) {
			if (count3List[i] == 0) {
				count3AverageList[i] = 10000;
			} else {
				count3AverageList[i] = num / count3List[i];
			}
			count3LastList[i] = num - count3LastList[i];
			count3ModList[i] = count3LastList[i]/count3AverageList[i];
			if (count3ModList[i]>maxMod){
				maxMod=count3ModList[i];
			}
		}
		br.close();
		writeCount3(count3Str);

	}

	public void writeCount3(String count3Str) throws Exception {
		FileWriter fw = new FileWriter(count3Str);
		BufferedWriter bw = new BufferedWriter(fw);
		for (int i = 0; i < 1000; i++) {

			String line = getIndex(i);
			line = line + "\t" + count3List[i] + "\t" + count3AverageList[i] + "\t" + count3LastList[i]+"\t" + count3ModList[i];
			System.out.println(line);
			bw.write(line);
			bw.newLine();
		}
		bw.close();
	}

	public void add(String line) {

		int index = getIndex(line);

		count3List[index]++;
	}

	public void addLast(String line, int num) {
		int index = getIndex(line);

		count3LastList[index] = num;
	}

	public static int getIndex(String line) {
		String[] stringArray = line.split(" ");
		int one = Integer.parseInt(stringArray[2]);
		int two = Integer.parseInt(stringArray[3]);
		int three = Integer.parseInt(stringArray[4]);
		int index = one * 100 + two * 10 + three;
		// System.out.println("line is "+line+" index is "+index);
		return index;
	}

	public static String getIndex(int index) {
		if (index < 10) {
			return "00" + index;
		}
		if (index < 100) {
			return "0" + index;
		}
		if (index < 1000) {
			return String.valueOf(index);
		}
		return null;
	}

	public int[] getCount3ModList() {
		return count3ModList;
	}

	public void setCount3ModList(int[] count3ModList) {
		this.count3ModList = count3ModList;
	}

}

